package com.xmut.mall.auth.service.impl;

import com.xmut.mall.auth.constant.AuthConstant;
import com.xmut.mall.auth.feign.MemberFeignService;
import com.xmut.mall.auth.service.RegisterService;
import com.xmut.mall.auth.utils.RespBean;
import com.xmut.mall.auth.vo.RegisterVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class RegisterServiceImpl implements RegisterService {

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private MemberFeignService memberFeignService;
    /**
     * 注册
     * @param vo
     * @return
     */
    @Override
    public RespBean register(RegisterVo vo) {
        String redisCode = redisTemplate.opsForValue().get(AuthConstant.SMS_CODE_CACHE_PREFIX + vo.getMobilePhone());
        if(!redisCode.equals(vo.getCode())){
            // 如果验证码和redis中的不一致
            return RespBean.error("验证码错误！");
        }


        // 开始注册
        RespBean registerResult = memberFeignService.registerUser(vo);
        if(registerResult.getCode() == 201){
            // 注册成功
            return RespBean.success("注册成功");
        }else {
            return RespBean.error(registerResult.getMessage());
        }
    }
}
